package com.zhdl.network.servertcp;

import cn.hutool.core.util.HexUtil;
import com.project.command.faq.analysis.ComAnalysis;
import com.project.command.faq.analysis.ComAnalysisData;
import com.project.command.faq.analysis.ComAnalysisResult;
import com.project.common.constant.Dic;
import com.zhdl.network.modbus.dto.ModbusDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 手操器ModbusTcp响应数据,解析
 */
@Slf4j
@Service(Dic.HAND_MODBUS_RTU_ANALYSIS)
public class HandModBusRtuAnalysisImpl implements ComAnalysis<ModbusDto, List<ModbusDto>> {

    @Override
    public ComAnalysisResult<ModbusDto> analysisData(ComAnalysisData<List<ModbusDto>> data) {
        List<ModbusDto> modbusFrames = data.getBuf();
        if (modbusFrames.size() > 0) {
            log.debug("拼出完整数据包个数:" + modbusFrames.size());
        }
        return new ComAnalysisResult<>(modbusFrames);
    }

    @Override
    public String getHeadAnalysis() {
        // tcp 手操器信息
        return "";
    }

    @Override
    public byte[] getAfterAnalysis(String cmdStr) {
        // 头信息+数据长度+功能码+ 字节数
        String cmd = (getHeadAnalysis() + cmdStr).toUpperCase();
        return HexUtil.decodeHex(cmd);
    }
}
